import { App } from "vue"
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw, Router } from "vue-router"

export const routes:RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home.vue'),
    meta: {
      title: ''
    },
  },
	{
    path: '/page01',
    name: 'page01',
    component: () => import('@/views/page01.vue'),
    meta: {
      title: ''
    },
  }
]

export const router = createRouter({
  history: createWebHistory(),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { left: 0, top: 0 }
    }
  },
  routes,
})

// 路由守卫
function createRouterGuard (router: Router) {
  // 全局前置守卫
  router.beforeEach((to, from) => {
		// 添加参数
    const pid = to.query.pid || from.query.pid || ''
    if (pid) {
      to.query.pid = pid
    }
  })

  router.afterEach((to, from) => {
    // 全局后置守卫
  })
}

export async function setupRouter(app: App) {
  // 挂载路由
  app.use(router)

  // 守卫
  createRouterGuard(router)

  // 路由准备就绪
  await router.isReady()
}


